<html>
<head><meta charset="utf-8"><title>Adding bool::{either, then_either} · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html">Adding bool::{either, then_either}</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231157028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231157028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231157028">(Mar 20 2021 at 16:48)</a>:</h4>
<p>Hey, what would people think about adding the following methods to <code>bool</code>? I don’t know about anyone else but I end up writing <code>let a = if b { c } else { d };</code> a lot in my code, and it would be nice if we could reduce that down to one line since rustfmt will always expand that snippet to five lines no matter how short it is. This would also be incredibly useful in configuration heavy code.</p>
<p>This kind of adjacent to <code>bool::then</code> except for when I know what the value for both branches should be.</p>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=5d5424f55c39bb14f0c0f4ac0188e99b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=5d5424f55c39bb14f0c0f4ac0188e99b</a></p>



<a name="231157338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231157338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Duchniewicz <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231157338">(Mar 20 2021 at 16:54)</a>:</h4>
<p>Probably tangential to the discussion, but there are some over-verbosities, simillar to this where rustfmt does this expansion even if the operation is trivial</p>



<a name="231157477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231157477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Duchniewicz <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231157477">(Mar 20 2021 at 16:56)</a>:</h4>
<p>I can see the usage for <code>either</code> but the other function’s usage eludes me</p>



<a name="231157495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231157495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BurntSushi <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231157495">(Mar 20 2021 at 16:56)</a>:</h4>
<p>IMO, adding APIs motivated by sub-optimal rustfmt behavior doesn't seem like a great idea to me...</p>
<p>also, i _think_ this is why I use <code>use_small_heuristics = "max"</code>: <a href="https://github.com/rust-lang/regex/blob/ff283badce21dcebd581909d38b81f2c8c9bfb54/rustfmt.toml#L2">https://github.com/rust-lang/regex/blob/ff283badce21dcebd581909d38b81f2c8c9bfb54/rustfmt.toml#L2</a> --- although, oddly, i can't find any docs for <code>use_small_heuristics</code> at <a href="https://github.com/rust-lang/rustfmt/blob/master/Configurations.md">https://github.com/rust-lang/rustfmt/blob/master/Configurations.md</a></p>



<a name="231158272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231158272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231158272">(Mar 20 2021 at 17:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="222471">BurntSushi</span> <a href="#narrow/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D/near/231157495">said</a>:</p>
<blockquote>
<p>although, oddly, i can't find any docs for <code>use_small_heuristics</code> at <a href="https://github.com/rust-lang/rustfmt/blob/master/Configurations.md">https://github.com/rust-lang/rustfmt/blob/master/Configurations.md</a></p>
</blockquote>
<p>The <code>master</code> branch is rustfmt 2.0, not the 1.x version distributed via rustup. That option is probably renamed/replaced.</p>



<a name="231158373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231158373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231158373">(Mar 20 2021 at 17:11)</a>:</h4>
<p>Configuration options are documented at <a href="https://rust-lang.github.io/rustfmt/?version=v1.4.36&amp;search=">https://rust-lang.github.io/rustfmt/?version=v1.4.36&amp;search=</a></p>
<p>I believe I saw there was intent to move master back to 1.4, but I am uncertain what the status is.  It is a bit confusing.</p>



<a name="231166006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231166006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231166006">(Mar 20 2021 at 19:20)</a>:</h4>
<blockquote>
<p>IMO, adding APIs motivated by sub-optimal rustfmt behavior doesn't seem like a great idea to me...</p>
</blockquote>
<p>Well that was an example, maybe a poor one, but I still think it’s annoying having to use full <code>if else</code> syntax, and it would nice to have something shorter. In other languages this solved by the ternary operator. E.g. <code>let a = b ? c : d;</code>. This provides similar functionality to that.</p>



<a name="231166298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231166298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231166298">(Mar 20 2021 at 19:25)</a>:</h4>
<blockquote>
<p>I can see the usage for <code>either</code> but the other function’s usage eludes me</p>
</blockquote>
<p><span class="user-mention" data-user-id="344196">@Jakub Duchniewicz</span> It’s the same as <code>either</code> but the values are lazily initialised. You can see in my example of <code>then_either</code> only one of those strings is actually allocated.</p>
<p>It’s the same reason there’s <code>unwrap_or</code> and <code>unwrap_or_else</code>.</p>



<a name="231166586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231166586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231166586">(Mar 20 2021 at 19:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="374396">hyd-dev</span> <a href="#narrow/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D/near/231158272">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="222471">BurntSushi</span> <a href="#narrow/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D/near/231157495">said</a>:</p>
<blockquote>
<p>although, oddly, i can't find any docs for <code>use_small_heuristics</code> at <a href="https://github.com/rust-lang/rustfmt/blob/master/Configurations.md">https://github.com/rust-lang/rustfmt/blob/master/Configurations.md</a></p>
</blockquote>
<p>The <code>master</code> branch is rustfmt 2.0, not the 1.x version distributed via rustup. That option is probably renamed/replaced.</p>
</blockquote>
<p>It's called <code>width_heuristics</code> in rustfmt 2.0</p>



<a name="231207963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231207963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Duchniewicz <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231207963">(Mar 21 2021 at 10:39)</a>:</h4>
<p>I think there is <a href="https://github.com/rust-lang/rfcs/issues/1362">rationale behind not having ternary operator</a>. This operator seems to be quite abusable and maybe that is the reason Rust decided to opt-out from it</p>



<a name="231213474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231213474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231213474">(Mar 21 2021 at 12:26)</a>:</h4>
<p><span class="user-mention" data-user-id="344196">@Jakub Duchniewicz</span> I’m not sure how that issue relates. This method achieves the same effect as the common use case for <code>?:</code>, it doesn’t have the same downsides of the operator as a method. It’s shorter than <code>if cond { } else { }</code>, but longer and clearer than <code>?:</code> and is not chainable in the same way people use <code>?:</code>.</p>



<a name="231213679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231213679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Duchniewicz <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231213679">(Mar 21 2021 at 12:30)</a>:</h4>
<p>Yes, just mentioning this to give a broader context</p>



<a name="231213772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231213772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Duchniewicz <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231213772">(Mar 21 2021 at 12:32)</a>:</h4>
<p>Maybe there could be a Rust alternative like a trait making a type <code>Ternary</code>able, but that is just thinking aloud</p>



<a name="231213775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231213775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Duchniewicz <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231213775">(Mar 21 2021 at 12:32)</a>:</h4>
<p>Probably digressing though</p>



<a name="231217875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231217875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231217875">(Mar 21 2021 at 13:57)</a>:</h4>
<p>I believe version=Two property of rustfmt will make it put if/else on one line sometimes.</p>



<a name="231230387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20bool%3A%3A%7Beither%2C%20then_either%7D/near/231230387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20bool.3A.3A.7Beither.2C.20then_either.7D.html#231230387">(Mar 21 2021 at 17:49)</a>:</h4>
<p>I don't think that you can construct any valid argument for <code>?:</code> being abusable but <code>if-else</code> <em>not</em> being equally as abusable.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>